﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WarehouseManagement.BusinessLogic.BC;
using System.Linq;
using WarehouseManagement.BusinessLogic.DTO;

namespace WarehouseManagement.Views.Customer
{
    public partial class CustomerList : DevExpress.XtraEditors.XtraForm
    {
        private CustomerBC _supplierBC;

        public CustomerList()
        {
            InitializeComponent();
            _supplierBC = new CustomerBC();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {

        }

        private void SupplierList_Load(object sender, EventArgs e)
        {

            BindCustomerList();
            grvCategoryList_FocusedRowChanged(null, null);
        }

        private void BindCustomerList()
        {
            var list = _supplierBC.GetCustomerDTOList().ToList();
            grdCategoryList.DataSource = list;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ShowAddEditForm(false);
        }
        private void ShowAddEditForm(bool editMode)
        {
            NewEditCustomer frm = new NewEditCustomer(editMode);
            if (editMode)
            {
                var obj = GetFocusObject();
                frm.Id = obj.Id;
                frm.Address = obj.Address;
                frm.BankAccount = obj.BankAccount;
                frm.BankName = obj.BankName;
                frm.ContactPerson = obj.ContactPerson;
                frm.Email = obj.Email;
                frm.Fax = obj.Fax;
                frm.FirstName = obj.FirstName;
                frm.LastName = obj.LastName;
                frm.Mobile = obj.Mobile;
                frm.Phone = obj.Phone;
                frm.SkypeAccount = obj.SkypeAccount;
                frm.YahooAccount = obj.YahooAccount;

            }
            frm.OnOperationCompleted = new NewEditCustomer.OperationCompleted(onOperationCompleted);
            frm.ShowDialog();
        }
        private void onOperationCompleted(bool success, bool isEditMode)
        {
            if (success)
            {
                BindCustomerList();
            }
            else
            {
                XtraMessageBox.Show("Đã xảy ra lỗi khi thực hiện thao tác", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ShowAddEditForm(true);
        }

        private void grvCategoryList_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var obj = GetFocusObject();
            btnDelete.Enabled = btnUpdate.Enabled = obj != null;
        }

        private CustomerDTO GetFocusObject()
        {
            return grvCategoryList.GetFocusedRow() as CustomerDTO;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Bạn có muốn xóa khách hàng này?", "Xóa khách hàng", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                return;
            try
            {
                var obj = GetFocusObject();
                _supplierBC.DeleteCustomer(obj.Id);
                BindCustomerList();
            }
            catch
            {
                XtraMessageBox.Show("Không thể xóa khách hàng lúc này", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                _supplierBC.RecycleContext();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}